package com.springcloud.market.api.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.market.api.entity.Order;
import com.springcloud.market.api.feign.StockFeignService;
import com.springcloud.market.api.feign.UserFeignClient;
import com.springcloud.market.api.service.OrderService;
//import com.springcloud.market.api.service.StockService;
//import com.springcloud.market.api.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RefreshScope
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private StockFeignService stockFeignService;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private OrderService orderService;

//   @DubboReference
//   private UserService userService;
//
//   @DubboReference
//   private StockService stockService;

    @RequestMapping("/add")
    public String add() {
        return stockFeignService.reduce();
    }

    @RequestMapping("/userInfo")
    public String addUser() {
        return userFeignClient.addUser();
    }

    @PostMapping("/addOrder")
    public String addOrder(@RequestBody Order order) {
        return orderService.createOrder(order);
    }

    @GetMapping("/createOrder")
    @SentinelResource(value = "orderApi", blockHandler = "handleFlowBlock")
    public String createOrder() {
        return "订单创建成功! (Time: " + System.currentTimeMillis() + ")";
    }
    public String handleFlowBlock(BlockException ex) {
        return "请求过于频繁，请稍后再试! (触发限流)";
    }


//    @RequestMapping("/getUserInfo")
//    public String getUserInfo(){
//        return userService.getUserInfo();
//    }
//
//    @RequestMapping("/getStockInfo")
//    public String getStockInfo(){
//        return stockService.getStockInfo();
//    }

    @RequestMapping("/readOrder")
    public String readOrder() {
        System.out.println("网关测试：order服务");
        return "网关测试：order服务";
    }

    @RequestMapping("/seata/add")
    public Order seataOrder(@RequestBody Order order) {
        return orderService.create(order);
    }

    @GetMapping("/createOrder2")
    public Order createOrder2() {
        return orderService.createOrder();
    }
}
